import {
  _decorator,
  Component,
  director,
  Label,
  Node,
  profiler,
  Sprite,
  tween,
  Vec3,
} from "cc";
import { gameConfig } from "./data/gameConfig";
import { layerRootAction } from "./layerRootAction";
import { bgmName, emits, local, pingtai } from "./data/enmus";
import { audioTool } from "./untils/audioTool";
import { save } from "./untils/tools";
import { wxAd } from "./AD/wxAd";
import { adMgr } from "./AD/adMgr";
const { ccclass, property } = _decorator;

@ccclass("mainView")
export class mainView extends Component {
  // 编辑器
  @property(Node) editRoot: Node;
  // 开场动画
  @property(Node) startAnima: Node;
  // 过关动画
  @property(Node) leveCatAnima: Node;
  // 关卡显示
  @property(Label) level: Label = null;
  // 三个功能按钮
  @property(Sprite) yichuSprite: Sprite;
  @property(Sprite) chehuiSprite: Sprite;
  @property(Sprite) shuaxinSprite: Sprite;
  // 开场动画
  // private startAnima_tween(callback: Function) {
  //     this.startAnima.active = true
  //     this.startAnima.setPosition(350, 0)
  //     tween(this.startAnima).to(0.6, { position: new Vec3(-350, 0, 0) }).call(() => {
  //         // 传入的形参
  //         callback()
  //     }).to(0.3, { position: new Vec3(-1000, 0, 0) }).call(() => {
  //         this.startAnima.active = false
  //     }).start()
  // }
  // 下一关动画
  private nextLevel_tween(callback: Function) {
    this.leveCatAnima.active = true;
    this.leveCatAnima.setPosition(500, 140);
    tween(this.leveCatAnima)
      .to(0.8, { position: new Vec3(-150, 140, 0) })
      .delay(1.2)
      .to(0.8, { position: new Vec3(-800, 140, 0) })
      .call(() => {
        // 传入的形参
        callback();
        this.leveCatAnima.setPosition(500, 140);
        this.leveCatAnima.active = false;
      })
      .start();
  }

  start() {
    // this.startAnima_tween(() => {
    //     this.init()
    // })
    this.init();
  }

  update(deltaTime: number) {}
  // 初始化
  init() {
    profiler.hideStats();
    // 监听音频中断
    if (gameConfig.system == pingtai.wx) {
      wxAd.ins.bgmZd(() => {
        audioTool.ins.playMusic(bgmName.music_bg);
      });
    }
    audioTool.ins.playMusic(bgmName.game_bg);
    gameConfig.gameRoot = this.node;
    this.level.string = String(gameConfig.nowLevel);
    director.on(emits.anewGame, this.anewGame, this);
    director.on(emits.daojuStatus, this.daojuStatus, this);
    // 道具状态
    // this.daojuStatus()
    // 显示模式
    if (gameConfig.gameModel == 0) {
      this.editRoot.active = false;
      this.startGame();
    } else {
      this.editRoot.active = true;
    }
    // 显示格子广告
    adMgr.hideBanner();
    adMgr.showGezi();
  }
  // 初始化并开始游戏
  startGame() {
    // 次数刷新
    gameConfig.fuhouNum = 1;
    gameConfig.yichuNum = 1;
    gameConfig.chehuiNum = 1;
    gameConfig.randomNum = 1;
    this.daojuStatus();
    let layerRoot = this.node
      .getChildByName("layerRoot")
      .getComponent(layerRootAction);
    layerRoot.startGame();
  }
  // 重新开始游戏
  anewGame() {
    // this.startAnima_tween(() => {
    //     this.init()
    // })
    this.init();
  }
  // 下一关
  nextLevel() {
    gameConfig.nowLevel += 1;
    save(local.nowLevel, gameConfig.nowLevel);
    this.nextLevel_tween(() => {
      this.level.string = String(gameConfig.nowLevel);
      this.startGame();
      // 插屏广告
      adMgr.showChaPing();
    });
  }
  // 关卡显示
  levelLabel() {
    this.level.string = String(gameConfig.nowLevel);
  }
  // 道具按钮状态
  daojuStatus() {
    if (gameConfig.yichuNum < 1) {
      this.yichuSprite.grayscale = true;
    } else {
      this.yichuSprite.grayscale = false;
    }
    if (gameConfig.chehuiNum < 1) {
      this.chehuiSprite.grayscale = true;
    } else {
      this.chehuiSprite.grayscale = false;
    }
    if (gameConfig.randomNum < 1) {
      this.shuaxinSprite.grayscale = true;
    } else {
      this.shuaxinSprite.grayscale = false;
    }
  }
  // 销毁组件
  protected onDestroy(): void {
    director.off(emits.anewGame, this.anewGame, this);
    director.off(emits.daojuStatus, this.daojuStatus, this);
  }
}
